package net.lgpage.util;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

public class ExpandleListUtil {

	public static void setSelection(ExpandableListView lv,int gp){
		ExpandableListAdapter adp = (ExpandableListAdapter) lv.getExpandableListAdapter();
		int realp = gp;
		for(int i =0;i<gp;i++){
			if(lv.isGroupExpanded(i)){
				realp+=adp.getChildrenCount(i);
			}
		}
		lv.setSelection(realp);
	}
	
	public static void setSelection(ExpandableListView lv, int gp,int cp){
		ExpandableListAdapter adp = (ExpandableListAdapter) lv.getExpandableListAdapter();
		if(!lv.isGroupExpanded(gp)){
			lv.expandGroup(gp);
		}
		int realp = gp;
		for(int i =0;i<gp;i++){
			if(lv.isGroupExpanded(i)){
				realp+=adp.getChildrenCount(i);
			}
		}
		realp+=cp;
		lv.setSelection(realp);
	}
	
	public static int getRealPosition(ExpandableListView lv,int gp){
		ExpandableListAdapter adp = (ExpandableListAdapter) lv.getExpandableListAdapter();
		int rp = 0;
		int max_gp = adp.getGroupCount();
		for(int i = 0;i<gp&&i<max_gp;i++){
			rp+=adp.getChildrenCount(i)+1;
		}
		return rp;
	}
	
	public static int getRealPotion(ExpandableListView lv,int gp,int cp){
		ExpandableListAdapter adp = (ExpandableListAdapter) lv.getExpandableListAdapter();
		int realp = 0;
		for(int i =0;i<gp;i++){
			realp+=adp.getChildrenCount(i);
		}
		realp+=cp;
		return realp;
	}
	
	public static int getSelectGroupPosition(ExpandableListView lv){
		int p = lv.getFirstVisiblePosition();
		ExpandableListAdapter adp = (ExpandableListAdapter) lv.getExpandableListAdapter();
		int sum = 0;
		for(int i = 0;i<adp.getGroupCount();i++){
			if(p>=sum&&p<sum+adp.getChildrenCount(i)){
				return i;
			}
			sum+=adp.getChildrenCount(i);
		}
		return 0;
	}
	
	
	
}
